<?php 
/**
*
 属性控制器 2016-11-9-22:41 @auth：liuchenghao
*/
namespace Admin\Controller;
use Think\Controller;

class AdvertiseController extends Controller{
       public function index()
    {
    
       $p=I('p')?I('p'):1;

    if(I('keywords')){
                $us = "advertisename like '%".I('keywords')."%'";
            }else{
                $us = '';
            }

    $userc=M('advertise');
   
    $a=$userc->where($us)->order('id')->page($p.',2')->select();
    
    $this->assign('list',$a);
   
    $count      = $userc->where($us)->count();
    $Page       = new \Think\Page($count,2);
    
    $show       = $Page->show();// 分页显示输出
    // var_dump($show);
    $this->assign('page',$show);// 赋值分页输出
    $this->display();
    }


       public function add()
    {
    
    $this->display();
    }
    public function insert()
    {
        
    $adv=M('advertise');    
    $upload = new \Think\Upload();// 实例化上传类    
    $upload->maxSize   =     3145728 ;// 设置附件上传大小    
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
    // 设置附件上传类型    
    $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    
    // 上传文件     
    $info   =   $upload->upload(); 
  // dump($info);exit;   
    if(!$info) 
    {// 上传错误提示错误信息       
     $this->error($upload->getError());   
    }else{// 上传成功   
     

          $img_path='./Uploads/'.$info['upload']["savepath"].$info['upload']['savename'];
          $adv->logo=$img_path;

         }
          if(empty($_POST['advertisename']))
        {
            $this->error('回去写数据',U('Admin/Advertise/add'));
            // $this->error('回去写数据',U('Admin/Administrator/edit'));
        }else{
        $adv->advertisename=I('advertisename');

        }

        $adv->status=I('status');

       
           if ($adv->add())
         {
            $this->success('添加成功!',U('Admin/Advertise/index'),2);
         }else{

            $this->error('添加失败!',U('Admin/Advertise/add'),2);
        }
         // $this->redirect('Admin/Adverise/index');
    }
  
         public function edit()     
        {
    
        $userc = M("advertise"); // 实例化User对象

        $this->assign('list',$userc->find(I('id')));
        
        $this->display();

        
       }
    

    public function update()
    {
  
    $adv=M('advertise');  
    $ss=$adv->find(I('get.id'));  
    $upload = new \Think\Upload();// 实例化上传类    
    $upload->maxSize   =     3145728 ;// 设置附件上传大小    
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
    // 设置附件上传类型    
    $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    
    // 上传文件     
    $info   =   $upload->upload(); 
  // dump($info);exit;   
    if(!$info) 
    {// 上传错误提示错误信息       
         $ac=$ss['logo'];
    }else{// 上传成功   
     // $this->error($upload->getError());   
     
 $ac='./Uploads/'.$info['upload']["savepath"].$info['upload']['savename'];
         
       

         }
         

         //接受名字
        
         //创建数据

        
         $data['advertisename']=I('post.advertisename');
          if(empty($data['advertisename']))
        {
            $this->error('回去写数据');
        }
         // $data['id']=I('get.id');
         $data['logo']=$ac;
        // var_dump($ss['logo']);
        // var_dump($data['logo']);
        // exit;
         // var_dump($data);
         // exit;
         $data['status']=I('post.status');


         $adv->where(array('id'=>I('get.id')))->save($data); 
        
        $this->redirect('Admin/Advertise/index');
        // if($adv->save()>0){
        //             $this->success('修改成功',U('Admin/Advertise/index'),3);
        //         }else{
        //             $this->error('修改失败');
        //         }
           // if($adv->create()<0){
           //      $this->error('cuole ',U('index'));
           // }else{
           //      if($adv->save()>0){
           //          $this->success('cg',U('index'));
           //      }else{
           //          $this->error('cuo');
           //      }
           // }
         
         //    $this->error('添加失败!',U('Admin/Advertise/add'),2);
         // }else{
         //    $this->success('添加成功!',U('Admin/Advertise/index'),2);

         // $this->redirect('Admin/Adverise/index');
    }

    public function delete()
    {
        $userc = M("advertise"); // 实例化User对象
        $id=I('get.id');
        // var_dump($id);
        $userc->delete($id); 
        $this->redirect('Admin/Advertise/index');

    }   
}
